import java.awt.TextArea;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;


public class FileTextArea extends TextArea {
	Etxt2DBGUI parent;
	File currentFile;
	
	public FileTextArea(Etxt2DBGUI parent, int rows, int columns){
		this(parent,null,rows,columns);
	}
	
	public FileTextArea(final Etxt2DBGUI parent,File file,int rows, int columns){
		super(rows,columns);
		currentFile = file;
		this.parent = parent;
		
		TextListener l = new TextListener(){
			@Override
			public void textValueChanged(TextEvent e) {
				if(!parent.queryTextArea.getTitleAt(parent.queryTextArea.getSelectedIndex()).startsWith("*")){
					parent.queryTextArea.setTitleAt(parent.queryTextArea.getSelectedIndex(),
						                        "*" + parent.queryTextArea.getTitleAt(parent.queryTextArea.getSelectedIndex()));
			
				}
			}
		};
		this.addTextListener(l);
		try{
			if(file!=null){
				StringBuffer fileData = new StringBuffer(1000);
		        BufferedReader reader = new BufferedReader(new FileReader(file));
		        char[] buf = new char[1024];
		        int numRead=0;
		        while((numRead=reader.read(buf)) != -1){
		            String readData = String.valueOf(buf, 0, numRead);
		            fileData.append(readData);
		            buf = new char[1024];
		        }
		        reader.close();
				setText(fileData.substring(0));
			}
		}catch(IOException ioe){
			ioe.printStackTrace();
		}
	}
	
	public File getFile(){
		return currentFile;
	}
	
	public void setFile(File file){
		currentFile = file;
	}
}
